package com.pet.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pet.mapper.CommentMapper;
import com.pet.mapper.TopicMapper;
import com.pet.pojo.Comment;
import com.pet.pojo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ForumServiceImpl implements ForumService{

    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Topic> findAllTopicList() throws ParseException {
        List<Topic> topicList = topicMapper.selectList(null);
        System.out.println(topicList);
        return topicList;
    }

    @Override
    public Topic findATopicById(Integer id) {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return topicMapper.selectOne(queryWrapper);
    }

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Comment> findAllCommentByTopicId(Integer id) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topic_id", id);
        return commentMapper.selectList(queryWrapper);
    }

    @Override
    public void saveComment(Comment comment) {
        commentMapper.insert(comment);
        Integer count = commentMapper.selectCount(null);
        System.out.println("count1 = " + count);
        Topic topic = findATopicById(comment.getTopicId());
        topic.setCount(count);
        System.out.println("count2 = " + count);
        topicMapper.updateById(topic);
    }
}
